/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.address;

import junit.framework.TestCase;

import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SipURIImplTest extends TestCase {
    public SipURIImplTest(String name) {
        super(name);
    }

    public void testSimpleCreation() {
        SipURIImpl uri = new SipURIImpl();
        assertEquals("The scheme is wrong!", "sip", uri.getScheme());
        assertNull("The user is not null!", uri.getUser());
        assertNull("The user password is not null!", uri.getUserPassword());
        assertNull("The host is not null!", uri.getHost());
        assertEquals("The port is not default!", 5060, uri.getPort());
    }

    public void testCreation() {
        final String USER = "alice";
        final String HOST = "yahoo.com";
        SipURIImpl uri = new SipURIImpl(USER, HOST);
        assertEquals("The scheme is wrong!", "sip", uri.getScheme());
        assertEquals("The user is wrong!", USER, uri.getUser());
        assertNull("The user password is not null!", uri.getUserPassword());
        assertEquals("The host is wrong!", HOST, uri.getHost());
        assertEquals("The port is not default!", 5060, uri.getPort());
    }

    public void testToString() {
        final String USER = "alice";
        final String HOST = "yahoo.com";
        SipURIImpl uri = new SipURIImpl(USER, HOST);
        uri.setPort(5062);
        uri.setSecure(true);
        try {
            uri.setUserPassword("secret");
        } catch (ParseException e) {
            fail("Cannot set password for the URI: " + e.getMessage());
        }
        try {
            uri.setParameter("custom", "mine");
        } catch (ParseException e) {
            fail("Cannot set parameter for the URI: " + e.getMessage());
        }
        try {
            uri.setHeader("priority", "urgent");
        } catch (ParseException e) {
            fail("Cannot set header for the URI: " + e.getMessage());
        }
        assertEquals("The string form of the URI is wrong!", "sips:alice:secret@yahoo.com:5062;custom=mine?priority=urgent", uri.toString());
    }

    public void testLazyCreation() {
        final SipURIImpl uri = SipURIImpl.createLazily("sip:alice:secretword@atlanta.com:5061;transport=tcp");
        assertEquals("The scheme is wrong!", "sip", uri.getScheme());
        assertFalse("The URI must be not secure!", uri.isSecure());
        assertTrue("The URI must be SIP!", uri.isSipURI());
        assertEquals("The user is wrong!", "alice", uri.getUser());
        assertEquals("The user password is wrong!", "secretword", uri.getUserPassword());
        assertEquals("The host is wrong!", "atlanta.com", uri.getHost());
        assertEquals("The port is wrong!", 5061, uri.getPort());
        assertEquals("The parameter transport is wrong!", "tcp", uri.getParameter("transport"));
        assertEquals("The parameters are wrong!", "transport=tcp", uri.getParametersAsString("").trim());
    }

    public void testClone() {
        final SipURIImpl uri = SipURIImpl.createLazily("sip:alice:secretword@atlanta.com:5061;transport=tcp");
        final SipURIImpl newUri;
        try {
            newUri = (SipURIImpl) uri.clone();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Cannot clone the URI object: " + e);
            return;
        }
        assertEquals("The URI objects are not equal!", newUri, uri);
        assertEquals("The schemas are not equal!", newUri.getScheme(), uri.getScheme());
        assertEquals("The users are not equal!", newUri.getUser(), uri.getUser());
        assertEquals("The user passwords are not equal!", newUri.getUserPassword(), uri.getUserPassword());
        assertEquals("The hosts are not equal!", newUri.getHost(), uri.getHost());
        assertEquals("The ports are not equal!", newUri.getPort(), uri.getPort());
        assertEquals("The parameters are not equal!", newUri.getParametersAsString(""), uri.getParametersAsString(""));
    }
}
/*
 * $Log$
 */